import { defHttp } from '@/utils/http/axios';
import { LoginParams, LoginResultModel } from './model/userModel';

import { ErrorMessageMode } from '#/axios';
import { randomUUID } from '@/views/form-design/utils';

enum Api {
  Login = '/agent/UserLogin/login',
  Logout = '/agent/UserLogin/logout',
  LoginLog = '/agent/LoginLog/index',
  Pay = '/agent/safe/pay',
  PayList = '/agent/safe/payList',
  Interchange = '/agent/safe/interchange',
  Message = '/agent/Message/index',
  GetNewOrder = '/agent/newOrderReminder/newOrder',
  OpenAcceptOrder = '/agent/safe/acceptOrderStatus',
  OpenPrompt = '/agent/safe/prompt',
  GetNewReminder = '/agent/newOrderReminder/Reminder',
  GetSafe = '/agent/safe/index',
  SetPayPwd = '/agent/safe/payment',
  SetAccPwd = '/agent/safe/changePassword',
  GetGoogle = '/agent/google/index',
  EditGoogle = '/agent/google/edit',
  VerifyGoogle = '/agent/google/validation',
  ReadMsg = '/agent/Message/getMessageId',
  GetInfo = '/agent/index',
  PayStatus = '/agent/safe/getPayStatus',
  GetBalance = '/agent/safe/changeBalance',
  EditContact = '/agent/safe/contact',
  GetCode = '/api/Uniform/xcode',
}

export async function GetCodeApi() {
  let uuid = localStorage.getItem('uuid');
  if (!uuid) {
    uuid = randomUUID();
    localStorage.setItem('uuid', uuid);
  }

  const data = await defHttp.post(
    {
      url: Api.GetCode,
      headers: {
        'x-uuid': uuid,
      },
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );

  console.log(data);

  const blob = new Blob([data], { type: 'image/png' });
  return URL.createObjectURL(blob);
}

export function EditContactApi(contact: string) {
  return defHttp.post<any>({
    url: Api.EditContact,
    params: {
      contact,
    },
  });
}

export function GetBalanceApi() {
  return defHttp.post<any>({
    url: Api.GetBalance,
  });
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
      headers: {
        'x-uuid': localStorage.getItem('uuid'),
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function getUserInfo() {
  return defHttp.get<any>({
    url: Api.Login,
  });
}

export function getLoginLogApi(params: any) {
  return defHttp.post<any>({
    url: Api.LoginLog + `?page=${params.page}&limit=${params.pageSize}`,
    params,
  });
}

export function payApi(params: { id: number; amount: number }) {
  return defHttp.post<any>({
    url: Api.Pay,
    params,
  });
}

export function interchangeApi(params: {
  payment: string;
  payment_address: number;
  amount: number;
}) {
  return defHttp.post<any>(
    {
      url: Api.Interchange,
      params,
    },
    {
      isTransformResponse: false,
    },
  );
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout, params: { app: 'PC' } });
}

export function getMsg(params: { page: number }) {
  return defHttp.post({
    url: Api.Message + `?page=${params.page}`,
    params,
  });
}

export function GetNewOrderApi() {
  return defHttp.post({
    url: Api.GetNewOrder,
  });
}

export function OpenAcceptOrderApi(params: { status: number }) {
  return defHttp.post({
    url: Api.OpenAcceptOrder,
    params,
  });
}

export function OpenPromptApi(params: { prompt: number }) {
  return defHttp.post({
    url: Api.OpenPrompt,
    params,
  });
}

export function GetNewReminderApi(params: { type: string }) {
  return defHttp.post(
    {
      url: Api.GetNewReminder,
      params,
    },
    {
      errorMessageMode: 'none',
    },
  );
}
export function GetSafeApi() {
  return defHttp.post({
    url: Api.GetSafe,
  });
}
export function SetPayPwdApi(params: { oldPassword: string; newPassword: string }) {
  return defHttp.post({
    url: Api.SetPayPwd,
    params,
  });
}
export function SetAccPwdApi(params: { oldPassword: string; newPassword: string }) {
  return defHttp.post({
    url: Api.SetAccPwd,
    params,
  });
}
export function GetGoogleApi() {
  return defHttp.post({
    url: Api.GetGoogle,
  });
}
export function EditGoogleApi(params: { use_google: number; google_token?: string }) {
  return defHttp.post({
    url: Api.EditGoogle,
    params,
  });
}
export function VerifyGoogleApi(params: { type: string; googleCode: string }) {
  return defHttp.post({
    url: Api.VerifyGoogle,
    params,
  });
}

export function ReadMsgApi(params: { ids: number[] }) {
  return defHttp.post({
    url: Api.ReadMsg,
    params,
  });
}

export function getPayListApi() {
  return defHttp.post<any>({
    url: Api.PayList,
  });
}

export function GetInfoApi() {
  return defHttp.post<any>({
    url: Api.GetInfo,
  });
}

export function PayStatusApi(params: { order: string }) {
  return defHttp.post<any>({
    url: Api.PayStatus,
    params,
  });
}
